package com.rayboo.example.java8;

import java.util.StringJoiner;

/**
 * StringJoiner 示例
 *
 * @author ray
 * @since 2019/07/29
 *
 * <p>在java 8中，包中StringJoiner引入了一个新类java.util。
 * 使用这个类，我们可以使用指定的分隔符连接多个字符串，我们还可以在连接多个字符串时为最终字符串提供前缀和后缀。
 *
 * <p>https://beginnersbook.com/2017/10/java-8-stringjoiner/
 */
public class StringJoinerExample {

    public static void main(String[] args) {

        System.out.println("===== StringJoiner Example 1: Joining strings by specifying delimiter =====");
        // Passing Hyphen(-) as delimiter
        StringJoiner str1 = new StringJoiner("-");

        // Joining multiple strings by using add() method
        str1.add("Logan");
        str1.add("Magneto");
        str1.add("Rogue");
        str1.add("Storm");

        // Displaying the output String
        System.out.println(str1);

        System.out.println("===== StringJoiner Example 2: Adding prefix and suffix to the output String =====");
        /* Passing comma(,) as delimiter and opening bracket
         * "(" as prefix and closing bracket ")" as suffix
         */
        StringJoiner str2 = new StringJoiner(",", "(", ")");

        // Joining multiple strings by using add() method
        str2.add("Negan");
        str2.add("Rick");
        str2.add("Maggie");
        str2.add("Daryl");

        // Displaying the output String
        System.out.println(str2);

        System.out.println("===== StringJoiner Example 3: Merging two StringJoiner objects =====");
        /* Passing comma(,) as delimiter and opening bracket
         * "(" as prefix and closing bracket ")" as suffix
         */
        StringJoiner str3 = new StringJoiner(",", "(", ")");

        str3.add("Negan");
        str3.add("Rick");
        str3.add("Maggie");
        str3.add("Daryl");

        System.out.println("First String: "+str3);

        /* Passing hyphen(-) as delimiter and string "pre"
         * as prefix and string "suff" as suffix
         */
        StringJoiner stringJoiner = new StringJoiner("-", "pre", "suff");

        stringJoiner.add("Sansa");
        stringJoiner.add("Imp");
        stringJoiner.add("Jon");
        stringJoiner.add("Ned");

        System.out.println("Second String: "+stringJoiner);

        /* Merging both the strings
         * The important point to note here is that the output string will be
         * having the delimiter prefix and suffix of the first string (the string
         * which is calling the merge method of StringJoiner)
         */
        StringJoiner mergedString = str3.merge(stringJoiner);
        System.out.println(mergedString);


        System.out.println("===== StringJoiner Example: setEmptyValue(), length() and toString() methods =====");
        //Comma(,) as delimiter
        StringJoiner str4 = new StringJoiner(",");

        /* Using setEmptyValue() method, we can set the default value
         * of a StringJoiner instance, so if the StringJoiner is empty
         * and we print the value of it, this default value will be
         * displayed
         */
        str4.setEmptyValue("This is a default String");

        /* We have not added any string to StringJoiner yet so
         * this should display the default value of StringJoiner
         */
        System.out.println("Default String: "+str4);


        // Adding strings to StringJoiner
        str4.add("Apple");
        str4.add("Banana");
        str4.add("Orange");
        str4.add("Kiwi");
        str4.add("Grapes");
        System.out.println(str4);

        /* The length() method of StringJoiner class returns the
         * length of the string (the number of characters in the
         * StringJoiner instance)
         */
        int length = str4.length();
        System.out.println("Length of the StringJoiner: "+length);

        /* The toString() method is used for converting a StringJoiner
         *  instance to a String.
         */
        String s = str4.toString();
        System.out.println(s);
    }
}
